<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
    <style>
        .roletpl{
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
        }
    </style>
</head>

<body>
    <div id="container">
        <div id="formcontainer">
            <form class="layui-form layui-form-pane" id="editform" lay-filter="editform">
                <div class="layui-row">
                    <div class="layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">所属部门</label>
                            <div class="layui-input-block">
                                <input type="text" name="dept_id" id="dept_id" autocomplete="off" class="layui-input" value="0" lay-filter="dept_id" disabled>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">登录名</label>
                            <div class="layui-input-block">
                                <input type="text" name="login_name" autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" name="user_name" autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-collapse" lay-filter="role">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title">用户角色</h2>
                        <div class="layui-colla-content layui-show" id="view">
                            
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div id="opcontainer">
            <button class="layui-btn" onclick="HandleSave()">确认</button>
            <button class="layui-btn layui-btn-primary" onclick="HandleClose()">关闭</button>
        </div>
    </div>
</body>
<script type="text/html" id="roletpl">
    <div class="roletpl">
        {{# layui.each(d.roleList, function(index, item){ }}
             {{# if(item.regist){ }}
                <input type="checkbox" lay-filter="rolesetting" role_id={{ item.row_id }} user_id={{ d.row_id }} lay-skin="primary" checked title={{item.role_name}} />
            {{# } else { }}
                    <input type="checkbox" lay-filter="rolesetting" role_id={{ item.row_id }} user_id={{ d.row_id }} lay-skin="primary" title={{item.role_name}}  />
            {{# } }}
         {{# }); }}
    </div>
</script>
<script>
    var FormDetail = null;

    $(function() {
        LoadDetail();
    })

    function LoadDetail() {
        var param = {
            row_id: GetUrlParam("row_id")
        }
        SetForm("editform", "system/org/user/detail", param, function(data) {
            FormDetail = data;
            BindSelectTree("dept_id", "system/org/dept/tree", {}, FormDetail.dept_id);
            BindRole();
        })

    }

    function BindRole(){
        var getTpl = roletpl.innerHTML,view = document.getElementById('view');
        layui.laytpl(getTpl).render(FormDetail, function(html){
             view.innerHTML = html;
             layui.form.render();
        });
    }

    function HandleSave() {
        var user_id = GetUrlParam("row_id");
        var roleModelList = [];
        $("input[type='checkbox']").each(function(){
                var role_id = $(this).attr("role_id");
                var regist =  $(this).prop('checked');
                var setting = {
                    role_id:role_id,
                    regist:regist
                }
                roleModelList.push(setting);
        })
        var param = {
            user_id:user_id,
            roleModelList:roleModelList
        }
        HttpPost(JSON.stringify(param), 'system/org/rolesetting/updateUserRelation', function(result) {
                if (result.code == 0) {
                    OpenSuccess("设置成功",function(){
                        CloseDialog();
                    })
                   
                } else {
                    OpenFail(result.msg);
                }
            })
    }

    function BeforeSubmit(data) {
        return true;
    }


    function HandleClose() {
        CloseDialog();
    }
</script>

</html>